Type.registerNamespace('CoolCode.Web.Ajax.WebControls');

CoolCode.Web.Ajax.WebControls.StyledAutoCompleteBehavior = function(element)
{
	CoolCode.Web.Ajax.WebControls.StyledAutoCompleteBehavior.initializeBase(this, [element]);
}

CoolCode.Web.Ajax.WebControls.StyledAutoCompleteBehavior.prototype =
{
	/*initialize : function()
	{
		CoolCode.Web.Ajax.WebControls.StyledAutoCompleteBehavior.callBaseMethod(this, 'initialize');
	},

	dispose : function()
	{
		CoolCode.Web.Ajax.WebControls.StyledAutoCompleteBehavior.callBaseMethod(this, 'dispose');
	},*/

	get_itemTemplate : function()
	{
		return this._itemTemplate;
	},

	set_itemTemplate : function(value)
	{
		this._itemTemplate = value;
	},
	
	_update: function(prefixText, completionItems, cacheResults)
	{
        if (cacheResults && this.get_enableCaching()) {
            if (!this._cache) {
                this._cache = {};
            }
            this._cache[prefixText] = completionItems;
        }
       
        if (completionItems && completionItems.length) {
            this._completionListElement.innerHTML = '';
            
            this._selectIndex = -1;
            var _firstChild = null;
        
            for (var i = 0; i < completionItems.length; i++) {
                var itemElement = null;

                if (this._completionListElementID) { 
                    // the completion element has been created by the user and li won't necessarily work
                    itemElement = document.createElement('div');
                } else {
                    itemElement = document.createElement('li');
                }
                
                // set the first child if it is null
                if( _firstChild == null ){
                    _firstChild = itemElement;
                }

				if (this._itemTemplate && Array.isInstanceOfType(completionItems[i]))
				{
					itemElement.innerHTML = this._getItemHtml(completionItems[i]);
					this._usingItemTemplate = true;
					itemElement._text = completionItems[i][0];
                }
                else
                {
					itemElement.appendChild(document.createTextNode(this._getTextWithInsertedWord(completionItems[i])));
					this._usingItemTemplate = false;
                }
				
                // itemElement.__item = '';
                
                if (this._completionListItemCssClass) {
                    Sys.UI.DomElement.addCssClass(itemElement, this._completionListItemCssClass);
                } else {
                    var itemElementStyle = itemElement.style;
                    itemElementStyle.padding = '0px';
                    itemElementStyle.textAlign = 'left';
                    itemElementStyle.textOverflow = 'ellipsis';
                    // workaround for safari since normal colors do not
                    // show well there.
                    if (Sys.Browser.agent === Sys.Browser.Safari) {
                        itemElementStyle.backgroundColor = 'white';
                        itemElementStyle.color = 'black';
                    } else {
                        itemElementStyle.backgroundColor = this._textBackground;
                        itemElementStyle.color = this._textColor;
                    }
                }
                this._completionListElement.appendChild(itemElement);
            }
            var elementBounds = $common.getBounds(this.get_element());        
            this._completionListElement.style.width = Math.max(1, elementBounds.width - 2) + 'px';
            this._completionListElement.scrollTop = 0;
            
            this.raisePopulated(Sys.EventArgs.Empty);
            
            var eventArgs = new Sys.CancelEventArgs();
            this.raiseShowing(eventArgs);
            if (!eventArgs.get_cancel()) {
                this.showPopup();
                // Check if the first Row is to be selected by default and if yes highlight it and updated selectIndex.
                if (this._firstRowSelected && (_firstChild != null)) {
                    this._highlightItem( _firstChild );
                    this._selectIndex = 0;
                }
            }            
        } else {
            this._hideCompletionList();
        }
    },
    
    _getItemHtml : function(content)
    {
		var args = [this._itemTemplate];
		var length = content.length;
		for (var i = 0; i < length; i++)
		{
			args.push(content[i]);
		}
		return String.format.apply(null, args);
    },
    
    _setText: function(item)
    {
		var text = null;
		
        if (this._usingItemTemplate)
        {
			while (!item._text)
			{
				item = item.parentNode;
			}
	        
			text = this._getTextWithInsertedWord(item._text);
		}
		else
		{
			text = (item && item.firstChild) ? item.firstChild.nodeValue : null;
		}
        
        this.raiseItemSelected(new AjaxControlToolkit.AutoCompleteItemEventArgs(item, text));
        
        this._timer.set_enabled(false);

        var element = this.get_element();
        var control = element.control;
        if (control && control.set_text) {
            control.set_text(text);
            $common.tryFireEvent(control, "change");
        }
        else {
            element.value = text;
            $common.tryFireEvent(element, "change");
        }
        this._currentPrefix = this._currentCompletionWord();
        this._hideCompletionList();
    }
}

CoolCode.Web.Ajax.WebControls.StyledAutoCompleteBehavior.registerClass('CoolCode.Web.Ajax.WebControls.StyledAutoCompleteBehavior', AjaxControlToolkit.AutoCompleteBehavior);
